$NG.AllReady(function (page, { useDataIndexChange, useUpdateRow,useValuesChange , useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_visit_plan_m")
    const grid = $NG.getCmpApi("p_form_visit_plan_d")
    let cityMap={}
    let phidPc = null
    let gxrArr = []
    let gxrScArr = []
    getAllGxr()
    getGxrInfo()
    $NG.updateUI(updater => {
        updater.editGrid.p_form_visit_plan_d.u_jump.setProps((p)=>{
            return {
                ...p, editor: {
                  ...p.editor,
                  onDoubleClick(){
                    const title = "现场拜访记录-新增"
                    const origin = window.location.href.split("#")[0]
                    const arrD = grid.getRows()
                    let it = null;
                    const _tempArr = []
                    arrD.forEach((item, index) => {
                        if(item.checked){
                            _tempArr.push(index+1)
                            it = item
                        }
                    })
                    if(it){
                        if(_tempArr.length > 1){
                            $NG.alert('查看详细请勿选择多行！')
                        } else if(_tempArr.length == 1){
                            const param = `#/customform/detail?otype=add&busType=EFORM9000000092&isSso=1&AppTitle=${encodeURIComponent(title)}
                            &treeOcode=1&treeOcodeName=${encodeURIComponent(document.querySelector(".ant-select-selection-item")?.textContent) || ""}
                                            &treePc=&treePcName=&username=${it.u_plan_visit_ps_EXName}&uid=${it.u_plan_visit_ps}`
                            $NG.open(origin+param)
                        }
                    }
                    console.log('双击成功!!!!!', window.location.href);
                  }
                }
            }
        })
    })
    $NG.execServer("getInDate", {}, function (res){
        if(res.count == 0){
            $NG.alert("当前日期不在任何工作周期范围内")
        }else{
            console.log(res);
            const _data = JSON.parse(res.data)[0].extendObjects
            console.log(`获取当前的工作周期`,_data);
                $NG.updateState((updater)=>{
                    updater.data.p_form_visit_plan_m.setProps({
                        phid_cycle:{
                                label:_data.bill_name,
                                value:_data.phid
                            }
                        })
                })
        }
    })
    useValuesChange(({args})=>{
        console.log(`监听周期`,args[0]);
    })
    // 监听表格行为
    useUpdateRows(({args})=>{
        console.log(`表格行为监听`, args);
        setButton()
        const row = args[0]
        let lock = false
        row.forEach(item => {
            if(item.u_visit_way !== '现场回访'){
                item.u_visit_way = "现场回访"
                lock = true
            }
        })
        if(lock){
            grid.refreshView()

        }
    },"p_form_visit_plan_d")
    // 监听表体编辑
    useUpdateRow(({args})=>{
        // gxrDFilter(args[0].phid_pc)
        console.log(`监听编辑`, args);
        if(args[0].phid_pc != phidPc){
            phidPc = args[0].phid_pc
            args[0].u_plan_visit_ps = u_plan_visit_ps=null
            args[0].u_plan_visit_ps_EXName = null
            console.log(args[0].phid_pc);
            gxrDFilter(args[0].phid_pc)
            console.log(`gxrArr`, gxrArr);
        }
        gxrArr.forEach(item=>{
            if(item.u_gxr == args[0].u_plan_visit_ps){
                console.log(cityMap[item.pno],cityMap[item.cno]);
                args[0].u_provice = item.pno
                args[0].u_provice_EXName = cityMap[item.pno]
                args[0].u_city = item.cno
                args[0].u_city_EXName = cityMap[item.cno]
                args[0].u_telephine = item.u_mobile_phone
                args[0].u_khdw = item.gxr_dwmc
            }
        })
        console.log(`gxrScarr`,gxrScArr);
        gxrScArr.forEach((item)=>{
            if(item.u_gxr == args[0].u_plan_visit_ps){
                args[0].u_schfqk = item.u_hfqk
                args[0].u_s_visit_time = item.u_time
            }
        })
        grid.refreshView()
    },"p_form_visit_plan_d")

    // 监听事业部的变化
    useValuesChange(({args})=>{
        proDFilter()
        getGxrInfo()
        getGxrScInfo()
    }, "p_form_visit_plan_m.cur_orgid")
    // 按钮属性说明的设置
    function setButton(){
        const _arrD = grid.getRows()
        let _lock = false
        _arrD.forEach(item => {
            if(item.u_jump !== '双击查看'){
                item.u_jump = '双击查看'
                _lock = true
            }else {}
        })
        if(_lock){
            grid.refreshView()
        }
    }
        // 获取省份信息
        function getAllGxr(){
            $NG.execServer("getAllCity", {}, function(res){
                const _data = JSON.parse(res.data)
                const _map = {}
                _data.forEach(item =>{
                    const datas = item.extendObjects
                    _map[datas.pid] = datas.pname
                    _map[datas.cno] = datas.cname
                })
                cityMap = {..._map}
            })
        }
        function proDFilter(){
            const phid_pcc = $NG.getCmpApi('p_form_visit_plan_m').getItem('cur_orgid').getValue();
            if(!phid_pcc){
              return 
            }
            $NG.updateUI(updater => updater.editGrid.p_form_visit_plan_d.phid_pc.setProps((p) => {
              return {
                ...p, editor: {
                  ...p.editor,
                  clientSqlFilter: { phid_org: $NG.getCmpApi('p_form_visit_plan_m').getItem('cur_orgid').getValue() },
                }
              };
            }));
        }
        function gxrDFilter(phid){
            const phid_pcc = phid
            if(!phid_pcc){
              return 
            }
            $NG.updateUI(updater => updater.editGrid.p_form_visit_plan_d.u_plan_visit_ps.setProps((p) => {
                console.log(phid);
              return {
                ...p, editor: {
                  ...p.editor,
                  clientSqlFilter: { phid_pc: phid },
                }
              };
            }));
        }
        
        // 获取干系人信息
    function getGxrInfo(arg){
        if(!mstform.getItem("cur_orgid").getValue()) return
        grid.refreshView()
        $NG.execServer("getAllFilterGxr", {phid_org:mstform.getItem("cur_orgid").getValue()},function(res){
            console.log(`获取干系人响应式数据`, res);
            if(res.count == 0 ) return
            const datas = JSON.parse(res.data)
            gxrArr = []
            datas.forEach(item => {
                gxrArr.push(item.extendObjects)
            })
        })
    }
    // 获取干系人上一次的记录
    function getGxrScInfo(){
        if(!mstform.getItem("cur_orgid").getValue()) return
        grid.refreshView()
        $NG.execServer("getGxrScInfo", {},function(res){
            console.log(`获取干系人响应式数据`, res);
            if(res.count == 0 ) return
            const datas = JSON.parse(res.data)
            gxrScArr = []
            datas.forEach(item => {
                gxrScArr.push(item.extendObjects)
            })
        })
    }
    // 格式化日期
    // 格式化日期
function formatDate(dateStr, outputFormat) {
    const date = new Date(dateStr);

    if (isNaN(date)) {
        return "无效的日期字符串";
    }

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');
    const hours = String(date.getHours()).padStart(2, '0');
    const minutes = String(date.getMinutes()).padStart(2, '0');

    let formattedDate;

    switch (outputFormat) {
        case "yyyy-mm-dd":
            formattedDate = `${year}-${month}-${day}`;
            break;
        case "yyyy-mm-dd HH:mm":
            formattedDate = `${year}-${month}-${day} ${hours}:${minutes}`;
            break;
        case "yyyy-mm":
            formattedDate = `${year}-${month}`;
        break;
        // 可以根据需要继续添加其他格式的处理逻辑
        default:
            formattedDate = "不支持的输出格式";
    }

    return formattedDate;
}
});